import pandas as pd
from las.client import LASClient
from las.exceptions import LASError
from las.auth import StaticCredentials
from las.task import SQLTask

# jobid = '2023110812'
# pt_date=jobid[0:8]
# # windows路径
# savepath = './csv/'

def execute_sql_task(ak,sk,sql):
    region = 'cn-beijing'
    endpoint = 'las.volcengineapi.com'
    client = LASClient(credentials=StaticCredentials(ak, sk), region=region,
                       endpoint=endpoint)


    try:
        # timeout 控制超时时间。。
        job = client.execute(task=SQLTask(name='sql task', query=sql, conf={}), is_sync=True,timeout=3000000)
        if job.is_success():
            result = job.get_result()
            return result
    except LASError as e:
        print(
            "Error in executing sql task. code = %s, error = %s" % ( e.code, e.info))

def analysis_detail(jobid,savepath):
    try:
        ak = "AKLTNjI3Y2IxNzc1OGFiNGE5ZmI0YWM1Mzc5MzE3N2IxNzY"
        sk = "TkRJNE56SmxPR1EwTnpNeE5HUmlaR0V5WWpFMU5tRTVOelptWkRNM01tSQ=="

        sql = """
     select
    job_id,
    description,
    faultid,
    faultobj,
    job_isPass,
    functionname,
    faultdesp,
    test_desp
    from dwd.dwd_sim_fault_injection_case_d
    where pt_jobid='{}'
              """
        sql = sql.format(jobid)
        print("sql: " + sql)
        result = execute_sql_task(ak, sk, sql)
        df_injt = pd.DataFrame(result.data[1:])
        df_injt.columns = result.data[0]

        # 目的是格式化时间并增加send_time 字段。
        if df_injt.shape[0] > 0:
            df_injt.to_pickle(savepath + 'data_detail_' + jobid + '.pkl')
    except Exception as e:
        print('data parse eth error, ', str(e))


if __name__ == '__main__':
    jobid = '2023110812'
    savepath = './'
    analysis_detail(jobid,savepath)


